﻿using MediatR;
using SystemLYH.API.Application.Command.QualityCommand;
using SystemLYH.Domain;
using SystemLYH.Domain.Dto;
using SystemLYH.Infrstructre.Impl;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.QualityHandlers
{
    public class QualityShowHandlers : IRequestHandler<QualityShowCommand, PageDTO<Qualityduring>>
    {
        protected readonly IQualityRepository _qualityRepository;
        /// <summary>
        /// 库位构造函数
        /// </summary>
        /// <param name="qualityRepository"></param>
        public QualityShowHandlers(IQualityRepository qualityRepository)
        {
            _qualityRepository = qualityRepository;
        }
        public async Task<PageDTO<Qualityduring>> Handle(QualityShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Qualityduring> page = new PageDTO<Qualityduring>();
            var list = (await _qualityRepository.GetAllAsync()).Where(x => !x.IsDel);
            //质检编号
            if (request.QualityNumber != null)
            {
                list = list.Where(x => x.QualityNumber == request.QualityNumber);
            }
            //生产线编号
            if (request.productionlineNumber != null)
            {
                list = list.Where(x => x.productionlineNumber == request.productionlineNumber);
            }
            //状态
            if (request.QualityState != null)
            {
                list = list.Where(x => x.QualityState == request.QualityState);
            }
            page.pageRoot = list.Count();
            page.pageCount = (int)Math.Ceiling(page.pageRoot * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x => x.QualityId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return page;
        }
    }
}
